#include "MusicPlayer.h"
#include "MusicEnum.h"


using namespace std;
using namespace sf;
MusicPlayer::MusicPlayer(): 
mMusic()
, mFilenames()
, mVolume(100.f)

{
	cout << Level::getMusicPath(MENUTHEME) << endl;
	mFilenames[MENUTHEME] = Level::getMusicPath(MENUTHEME);
	mFilenames[MISSIONSOUND] = Level::getMusicPath(MISSIONSOUND);
	mFilenames[INGAMETHEME] = Level::getMusicPath(INGAMETHEME);
	mFilenames[WINGAMETHEME] = Level::getMusicPath(WINGAMETHEME);
	mFilenames[LOOSEGAMETHEME] = Level::getMusicPath(LOOSEGAMETHEME);

	mFilenames[TANKFIRE] = Level::getMusicPath(TANKFIRE);
	mFilenames[COLLISIONENEMY] = Level::getMusicPath(COLLISIONENEMY);
	mFilenames[WINSOUND] = Level::getMusicPath(WINSOUND);
	mFilenames[LOOSESOUND] = Level::getMusicPath(LOOSESOUND);
	mFilenames[TYPEWRITTERSOUND] = Level::getMusicPath(TYPEWRITTERSOUND);
	mFilenames[ENEMYFIRE] = Level::getMusicPath(ENEMYFIRE);



	SoundBufferHolder[TANKFIRE].loadFromFile(mFilenames[TANKFIRE]);
	SoundBufferHolder[COLLISIONENEMY].loadFromFile(mFilenames[COLLISIONENEMY]);
	SoundBufferHolder[WINSOUND].loadFromFile(mFilenames[WINSOUND]);
	SoundBufferHolder[LOOSESOUND].loadFromFile(mFilenames[LOOSESOUND]);
	SoundBufferHolder[TYPEWRITTERSOUND].loadFromFile(mFilenames[TYPEWRITTERSOUND]);
	SoundBufferHolder[ENEMYFIRE].loadFromFile(mFilenames[ENEMYFIRE]);
	
}

void MusicPlayer::play(MusicEnum theme)
{
	std::string filename = mFilenames[theme];
	if (!mMusic.openFromFile(filename))
	{
		//error
	}
	mMusic.setVolume(mVolume);
	mMusic.setLoop(true);
	mMusic.play();
}

void MusicPlayer::stop()
{
	mMusic.stop();
}
void MusicPlayer::setPaused(bool paused)
{
	if (paused)
		mMusic.pause();
	else
		mMusic.play();

	
}

void MusicPlayer::playSound(MusicEnum sound)
{
	mSounds.push_back(sf::Sound(SoundBufferHolder[sound]));
	mSounds.back().play();
}

void MusicPlayer::stopSound(MusicEnum sound)
{
	
	mSounds.back().stop();
}

void MusicPlayer::removeStoppedSounds()
{
	mSounds.remove_if([](const sf::Sound& s)
	{
		return s.getStatus() == sf::Sound::Stopped;
	});
}

int MusicPlayer::getSoundDuration(MusicEnum sound)
{
	return int(SoundBufferHolder[sound].getDuration().asMilliseconds());
}